unit ICQ_RemoteCP_Log;

interface

uses
 gnugettext,
 JvLogFile;

const
 //log settings
 cLogUse = $1000;
 cLogInit = $0001;
 cLogConn = $0002;
 cLogMsg = $0004;
 cLogCmd = $0008;
 cLogAnsw = $0010;
 cLogDld = $0020;
 cLogTimeNone = 0;
 cLogTime05 = 1;
 cLogTime10 = 2;
 cLogTime60 = 3;
 cLogDumpDefault = cLogTimeNone;

 //log strings
function cLogPlugin: String;
function cLogConnection: String;
function cLogMessage: String;
function cLogCommand: String;
function cLogAnswer: String;
function cLogError: String;
function cLogDownload: String;
//
function IsBitSet(Value, BitPlace: Integer): Boolean;
function IsBitsSet(Value: Integer; const Bits: array of const): Boolean;
//
procedure LogCreate(LoadFrom: String);
procedure LogSave(SaveTo: String);
procedure LogShow(Title: String);
procedure LogFree;
function LogActive: Boolean;
procedure Log(LogOptions: Integer; LogKeys: array of const; AReason, AText: String; TimeOnly: Boolean = false); overload;
procedure Log(AReason, AText: String; TimeOnly: Boolean = false); overload;

implementation

uses
 SysUtils,
 Wizard;

var
 LogFile: TJvLogFile = nil;

 {Log strings functions}

function cLogPlugin: String;
begin
 Result:=_('Plugin');
end;

function cLogConnection: String;
begin
 Result:=_('Connection');
end;

function cLogMessage: String;
begin
 Result:=_('Message');
end;

function cLogCommand: String;
begin
 Result:=_('Command');
end;

function cLogAnswer: String;
begin
 Result:=_('Answer');
end;

function cLogError: String;
begin
 Result:=_('Error');
end;

function cLogDownload: String;
begin
 Result:=_('Download');
end;

 {Log operation routines}

function IsBitSet(Value, BitPlace: Integer): Boolean;
begin
 Result:=(Value and BitPlace)>0;
end;

function IsBitsSet(Value: Integer; const Bits: array of const): Boolean;
 var
  i: Integer;
begin
 Result:=true;
 for i:=0 to High(Bits) do
  begin
   if Bits[i].VType=vtInteger then
     Result:=Result and IsBitSet(Value, Bits[i].VInteger);
  end;
end;

procedure LogCreate(LoadFrom: String);
begin
 LogFree;
 LogFile:=TJvLogFile.Create(nil);
 if FileExists(LoadFrom) then
   LogFile.LoadFromFile(LoadFrom);
end;

procedure LogSave(SaveTo: String);
begin
 if LogActive then
   LogFile.SaveToFile(SaveTo);
end;

procedure LogShow(Title: String);
begin
 if LogActive then
   LogFile.ShowLog(Title);
end;

procedure LogFree;
begin
 if LogActive then
  begin
   FreeAndNil(LogFile);
  end;
end;

function LogActive: Boolean;
begin
 Result:=Assigned(LogFile);
end;

procedure Log(LogOptions: Integer; LogKeys: array of const; AReason, AText: String; TimeOnly: Boolean = false);
 var
  i: Integer;
  LogText: String;
  LogDo: Boolean;
begin
 if LogActive then
  begin
   LogDo:=true;
   for i:=0 to High(LogKeys) do
     LogDo:=LogDo and IsBitSet(LogOptions, LogKeys[i].VInteger);
   if LogDo then
    begin
     LogText:=AText;
     ReplaceEx(LogText, #13, '_');
     ReplaceEx(LogText, #10, '_');
     ReplaceEx(LogText, '<', '(');
     ReplaceEx(LogText, '>', ')');
     if TimeOnly then
       LogFile.Add(TimeToStr(Time), AReason, LogText)
     else
       LogFile.Add(DateTimeToStr(Now), AReason, LogText);
    end;
  end;
end;

procedure Log(AReason, AText: String; TimeOnly: Boolean = false);
 var
  i: Integer;
  LogText: String;
begin
 if LogActive then
  begin
   LogText:=AText;
   ReplaceEx(LogText, #13, '_');
   ReplaceEx(LogText, #10, '_');
   ReplaceEx(LogText, '<', '(');
   ReplaceEx(LogText, '>', ')');
   if TimeOnly then
     LogFile.Add(TimeToStr(Time), AReason, LogText)
   else
     LogFile.Add(DateTimeToStr(Now), AReason, LogText);
  end;
end;

end.
